package base;

import javax.persistence.*;
import javax.sql.rowset.serial.SerialBlob;

import java.util.*;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class Test {
	
	
	public static void main(String[] arg){
			
			EntityManagerFactory emf= Persistence.createEntityManagerFactory("Tracker");	
			
			EntityManager em = emf.createEntityManager();    
			EntityTransaction transac = em.getTransaction();
			transac.begin();
			try{
				
			File newfile = new File(12, 25, "11");	
			File newfile1 = new File(7, 14, Integer.toString(12));
			File file = new File(1111, 2222, "3333dhezh");
			File file1 = new File(4444, 5555, "6666eferf");
			
			
			InetAddress ad = InetAddress.getByName("localhost");
			InetAddress ad1 = InetAddress.getByName("145.25.152.10");
			InetAddress ad2 = InetAddress.getByName("195.125.112.158");
			InetAddress ad3 = InetAddress.getByName("195.156.50.12");
			
			
			Peer newpeer = new Peer(ad, 2542);
			Peer newpeer1 = new Peer(ad1, 1245);
			Peer newpeer2 = new Peer(ad2, 8002);
			Peer peer2 = new Peer(ad3, 6002);
			
			Share s1 = new Share(Share.Stat.SEEDER,"Titi",  newfile, newpeer);
			newfile.addShare(s1);
			newpeer.addShare(s1);			
			Share s2 = new Share(Share.Stat.SEEDER,"Titi",  newfile1, newpeer1);
			newfile1.addShare(s2);
			newpeer1.addShare(s2);
			Share s3 = new Share(Share.Stat.LEECHER,"Tata",  file, newpeer2);
			newfile1.addShare(s3);
			newpeer1.addShare(s3);
			Share s4 = new Share(Share.Stat.SEEDER,"Tutu",  newfile, newpeer1);
			newfile1.addShare(s3);
			newpeer1.addShare(s3);
			Share s5 = new Share(Share.Stat.SEEDER,"Fofo",  file, peer2);
			file.addShare(s5);
			peer2.addShare(s5);
			Share s6 = new Share(Share.Stat.SEEDER,"AAAA",  file1, peer2);
			file1.addShare(s6);
			peer2.addShare(s6);
			
			
			em.persist(newfile);
			em.persist(newfile1);
			em.persist(file);
			em.persist(file1);
           	em.persist(newpeer);
           	em.persist(newpeer1);
           	em.persist(newpeer2);
           	em.persist(peer2);
           	em.persist(s1);               
           	em.persist(s2);
           	em.persist(s3);
           	em.persist(s4);
			em.persist(s5);	
			em.persist(s6);	
            transac.commit();
            
           
            
           /* 
            InetAddress i = InetAddress.getByName("145.25.152.10"); 
            System.out.println(i.getAddress());
            String string = new String(i.getAddress());
            
            Query z = em.createQuery("select p from Peer p where p.peerIP = :ip");
            z.setParameter("ip", i.getAddress());
            System.out.println(z.getResultList());
            
            */
           
	          
            
         					
			// Afficher la base 
			System.out.println("");
			System.out.println("BASE:");
			Query q1 = em.createQuery("select f from File f");
			Query q2 = em.createQuery("select p from Peer p");
			Query q3 = em.createQuery("select s from Share s");
			List<Peer> peers = q2.getResultList();
			List<File> files = q1.getResultList();
			List<Share> shares = q3.getResultList();
			
			for(File f:files)
				System.out.println(f);
			for(Peer p:peers)
				System.out.println(p);
			for(Share s: shares)
				System.out.println(s);
			
			System.out.println("");
			System.out.println("");
			
			
			//recherche fichier par nom
			System.out.println("Recherche par nom : Fofo");
			Query q4 = em.createQuery("select f from File f inner join f.shares s where s.fileName='Fofo'");
			try{
				File result1 = (File) q4.getSingleResult();
				System.out.println("Result :");
				System.out.println(result1);
			}
			catch(NonUniqueResultException e){
			}
			
			System.out.println("");
			System.out.println("");
			
			
			
			//Test recherche peer par key
			System.out.println("Recherche par peer par key : 3333dhezh ");
			Query q5 = em.createQuery("select p from Peer p inner join p.shares s where s.file.key='3333dhezh'");
			
				List<Peer> result2 = q5.getResultList();
				System.out.println("Result :");
				for(Peer p: result2)
					System.out.println(p);
								
			System.out.println("");
			System.out.println("");
			
			
			//Test recherche fichier par taille
			System.out.println("Recherche fichier par taille: >=2222");
			Query q6 = em.createQuery("select f from File f inner join f.shares s where f.fileLength>=2222");
			
				List<File> result3 = q6.getResultList();
				System.out.println("Result :");
				for(File f: result3)
					System.out.println(f);
								
			System.out.println("");
			System.out.println("");
			
			//Test recherche fichier par taille et nom;
			System.out.println("Recherche fichier par taille et nom : <2222 and Titi");
			Query q7 = em.createQuery("select distinct f from File f inner join f.shares s where f.fileLength<2222 and s.fileName='Titi'");
			
				List<File> result4 = q7.getResultList();
				System.out.println("Result :");
				for(File f: result4)
					System.out.println(f);
								
			System.out.println("");
			System.out.println("");
			
			
			 	em.close();
	         
	        
	           emf.close();
	        }
			catch(Exception e){}
	}
}
